package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    /**
     * 初始化账户记录；新增账户变动日志
     *
     * @param mapData {"userId",1,"title":"","amount":10,"orderNo":"cz001"}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserAccount(Map mapData) {
        //1.获取从MQ中获取到参数
        Long userId = (Long) mapData.get("userId");
        String title = (String) mapData.get("title");
        BigDecimal amount = (BigDecimal) mapData.get("amount");
        String orderNo = (String) mapData.get("orderNo");

        //2.保存账户记录
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(amount);
        userAccount.setAvailableAmount(amount);
        userAccount.setTotalIncomeAmount(amount);
        userAccountMapper.insert(userAccount);
        //3.新增账户变动日志
        this.saveUserAccountDetail(userId, title, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, amount, orderNo);
    }


    /**
     * 保存账户变动日志
     *
     * @param userId    用户ID
     * @param titile    内容
     * @param tradeType 交易类型 1201-充值 1202-锁定 1203-解锁 1204-消费
     * @param amount    金额
     * @param orderNo   订单编号
     */
    @Override
    public void saveUserAccountDetail(Long userId, String titile, String tradeType, BigDecimal amount, String orderNo) {
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setTitle(titile);
        userAccountDetail.setTradeType(tradeType);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    /**
     * 查询当前用户账户可用余额
     *
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getAvailableAmount(Long userId) {
        LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccount::getUserId, userId);
        queryWrapper.select(UserAccount::getAvailableAmount);
        UserAccount userAccount = userAccountMapper.selectOne(queryWrapper);
        if (userAccount != null) {
            return userAccount.getAvailableAmount();
        }
        return new BigDecimal("0.00");
    }

    /**
     * 扣减账户余额
     *
     * @param accountDeductVo
     * @return
     */
    @Override
    public void checkAndDeduct(AccountDeductVo accountDeductVo) {
        //1.扣减账户余额
        int rows = userAccountMapper.checkAndDeduct(accountDeductVo);
        if (rows == 0) {
            throw new RuntimeException("账户余额不足");
        }
        //2.新增账户变动日志
        this.saveUserAccountDetail(accountDeductVo.getUserId(), accountDeductVo.getContent(), SystemConstant.ACCOUNT_TRADE_TYPE_MINUS, accountDeductVo.getAmount(), accountDeductVo.getOrderNo());
    }
}
